package handle

import (
	"log"

	"github.com/apolloconfig/agollo/v4"
	"github.com/apolloconfig/agollo/v4/env/config"
	"github.com/apolloconfig/agollo/v4/storage"
)

var ApolloConfigCache *storage.Config

func InitApollo() {
	log.Println("开始初始化Apollo配置中心...")

	// 配置 Apollo 客户端
	c := &config.AppConfig{
		AppID:          cfg.ConfigCenter.AppID,
		Cluster:        cfg.ConfigCenter.Cluster,
		IP:             cfg.ConfigCenter.IP,
		NamespaceName:  cfg.ConfigCenter.NamespaceName,
		Secret:         cfg.ConfigCenter.Secret,
		IsBackupConfig: cfg.ConfigCenter.IsBackupConfig,
	}

	// 启动客户端
	client, err := agollo.StartWithConfig(func() (*config.AppConfig, error) {
		return c, nil
	})
	if err != nil {
		log.Fatalf("Failed to initialize Apollo client: %v", err)
	}

	log.Println("Apollo client initialized successfully")

	// 获取配置
	ApolloConfigCache = client.GetConfig(c.NamespaceName)
}

func init() {

}
